package cn.edu.zafu.bigdata.YinMingyan.chapter05;

import java.util.List;

/**
 * Provides a generic method to find the maximum value in a list.
 */
public class FindMax {

    /**
     * Finds the maximum element in the provided list.
     *
     * @param <T>   the type of elements in the list, which must implement the Comparable interface
     * @param list  the list of elements to search in
     * @return      the maximum element in the list
     * @throws IllegalArgumentException if the list is null or empty
     */
    public static <T extends Comparable<T>> T findMax(List<T> list) {
        if (list == null || list.isEmpty()) {
            throw new IllegalArgumentException("List must not be null or empty.");
        }
        T max = list.get(0);
        for (T element : list) {
            if (element.compareTo(max) > 0) {
                max = element;
            }
        }
        return max;
    }
}